﻿using System;
using System.Net;
using System.Net.Sockets;
using ItemQuoteLibrary;

namespace TcpSendComplexMessages_Server
{
    class Program
    {
        private static void Main(string[] args)
        {
            int communicationPort = 7;
            ItemQuoteDecoder itemEncoder = new ItemQuoteDecoderBin();

            TcpListener listener = new TcpListener(IPAddress.Any, communicationPort);
            listener.Start();

            TcpClient client = listener.AcceptTcpClient(); // Get client connection

            // Receive text-encoded quote
            ItemQuoteDecoder decoder = new ItemQuoteDecoderBin();
            ItemQuote quote = decoder.Decode(client.GetStream());
            Console.WriteLine("Received Text-Encoded Quote:");
            Console.WriteLine(quote);

            // Repeat quote with binary-encoding, adding 10 cents to the price
            ItemQuoteEncoder encoder = new ItemQuoteEncoderBin();
            quote.UnitPrice += 10; // Add 10 cents to unit price
            Console.WriteLine("Sending (binary)...");
            byte[] bytesToSend = encoder.Encode(quote);
            client.GetStream().Write(bytesToSend, 0, bytesToSend.Length);

            client.Close();
            listener.Stop();
        }
    }
}
